
class Game extends egret.Sprite {

	private static BmpTutorial0;

	private static BmpTutorial1 = "Main_BmpTutorial1";


	private var_314: egret.Sprite = null;

	private var_108: egret.Sprite = null;

	private var_106: number = 0;

	private var_119: Cell = null;

	private const_16: number = 10;

	private levelMgr: LevelMgr = null;

	private var_89: Cell = null;

	private var_110: egret.Bitmap = null;

	private var_171: egret.Sprite = null;

	private var_202: number = 3;

	private var_191: number = 0;

	private var_182: boolean = false;

	private levelCode: string = null;

	private var_162: number = -1;

	private var_265: number = -1;

	private const_8: egret.GlowFilter = new egret.GlowFilter(16776960, 0.7, 4, 4);

	private var_342: egret.Bitmap = null;

	private const_7: number = 1;

	private var_367: string = null;

	private curLevel: Level = null;

	private const_15: number = 20;

	// private var_340: Stat = null;

	private const_13: number = 2;

	private var_264: number = 0;

	private var_309: Achievements = null;

	private var_80: number = 0;

	private gameSprite: egret.Sprite = null;

	private const_4: number = 0;

	private var_207: number = 0;

	// private var_68: LevelEditor = null;

	public constructor() {
		super();
		// §§push(false);
		if (this.stage) {
			this.init();
		}
		else {
			this.addEventListener(egret.Event.ADDED_TO_STAGE, this.init, this);
		}
	}

	private method_233(isLevelPassed: boolean): void {
		this.method_157();
		this.method_117();
		GUI.removeAll();
		// §§push(true);
		if (this.var_106 == this.const_4) {
			// §§push(true);
		}
		if (this.var_106 == this.const_4) {
			// §§push(true);
		}
		else if (this.var_106 == this.const_7) {
			// §§push(true);
		}
		else if (isLevelPassed) {
			GUI.createSaveDialog(this.levelCode);
		}
		else {
			GUI.createnumbererLevelMenu(isLevelPassed, this.var_80, this.var_309);
		}
	}

	private onMouseMove(e: egret.TouchEvent): void {
		var cell: Cell = null;
		var g: egret.Graphics = null;
		if (this.var_182) {
			return;
		}


		if (e.target instanceof Cell) {
			cell = e.target as Cell;
			if (cell != this.var_119) {
				this.var_119 = cell;
				this.var_191 = 0;
				this.method_104(cell);
				this.var_119.filters = [this.const_8];
			}
		}
		else {
			this.method_104(null);
			if (this.var_119 != null) {
				this.var_119.filters = [];
				this.var_119 = null;
			}
		}
		if (this.var_89 || this.var_162 >= 0) {
			g = this.var_108.graphics;
			g.clear();
			// if (!e.buttonDown) {
			// 	this.var_89 = null;
			// 	return;
			// }
			this.method_276(e);
		}
	}

	public newAchievement(id: number): void {
		// if(this.var_68) {
		// }
	}

	public testEditorLevel(): void {
		var lvl: Level = null;
		// if (this.var_68) {
		// 	this.var_129 = this.var_68.getLevelCode();
		// 	if (this.var_129 == null) {
		// 		GUI.createSaveDialog(null);
		// 		return;
		// 	}
		// 	GUI.removeAll();
		// 	this.clearLevel();
		// 	lvl = new Level(this, "X");
		// 	lvl.initFromCode(this.var_129);
		// 	this.method_94(lvl);
		// 	GUI.showInGamePanel(this.var_26.levelName, this.var_26.maxPower, this.var_80, true);
		// 	this.var_106 = this.const_13;
		// }
	}

	private method_196(cell: Cell): void {
		this.var_89 = cell;
	}

	public levelEditorSave(): void {
		// if (this.var_68) {
		// 	GUI.createSaveDialog(this.var_68.getLevelCode());
		// }
	}

	private onEnterFrame(event: Event): void {
		if (this.var_182) {
			return;
		}
		if (this.curLevel.cpuTurnPeriod > 0) {
			this.var_207++;
			if (this.var_207 > this.curLevel.cpuTurnPeriod) {
				this.var_207 = 0;
				this.curLevel.onCpuTurn();
			}
		}
		this.var_264++;
		if (this.var_264 > this.const_15) {
			this.var_264 = 0;
			// _loc3_.var_80 = true;
			return;
			// §§push(this.var_80);
			// §§push(true);
		}
		else {
			this.var_191++;
			if (this.var_191 > this.const_16) {
				this.var_191 = 0;
				if (this.var_119) {
					// this.method_104(this.var_119);
					// §§push(true);
				}
				else {
					// this.method_117();
				}
			}
			return;
		}
	}

	private init(e: Event = null): void {
		// this.var_342 = new GUI.BmpBack();
		// this.addChild(this.var_342);
		this.gameSprite = new egret.Sprite();
		this.levelMgr = new LevelMgr(this);
		// this.addChild(this.var_314);
		this.addChild(this.gameSprite);
		// this.addChild(this.var_171);
		var levelData: string = null;// = GameAllianzApi.current_level;
		// §§push(true);
		// §§push(true);
		this.initLevel(0);
		if (levelData != null) {
			GUI.onStartSharedLevel(levelData, "Shared", "", "");
			// §§push(true);
		}
		else {
			// GameAllianzApi.addEventListener(GameAllianzApiEvent.RATE_LEVEL_CANCELED, this.method_177);
			// GameAllianzApi.addEventListener(GameAllianzApiEvent.RATE_LEVEL_DONE, this.method_177);
		}
	}

	private method_140(event: egret.TouchEvent): void {
		var cell: Cell = null;
		cell = event.target as Cell;
		// §§push(boolean(this.var_89));
		if (this.var_89 != cell) {
			this.curLevel.addLinkBetween(this.var_89, cell);
		}
		var g: egret.Graphics = this.var_108.graphics;
		g.clear();
		this.method_196(null);
	}

	private startGame(lvl: Level): void {
		this.clearLevel();
		this.curLevel = lvl;
		this.gameSprite.addChild(this.curLevel);
		this.levelCode = lvl.getCode();
		this.var_80 = 0;
		// this.method_117();
		// addEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
		// addEventListener(MouseEvent.MOUSE_DOWN, this.method_135);
		// addEventListener(MouseEvent.MOUSE_UP, this.method_140);
		this.addEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
	}

	public onLinkAttached(link: Link): void {
		// §§push(true);
		if (this.var_106 != this.const_4) {
			return;
		}
		if (this.levelMgr.currLevel() == 0) {
			this.method_190(1);
		}
		if (link.getOrigCell().type == this.var_202) {
			// §§push(link.getOrigCell().type);
		}
	}

	public clearTutorial(): void {
		GUI.hideHnumber();
		if (this.var_110) {
			this.var_110.parent.removeChild(this.var_110);
			this.var_110 = null;
		}
	}

	private method_157(): void {
		// removeEventListener(MouseEvent.MOUSE_MOVE, this.onMouseMove);
		// removeEventListener(MouseEvent.MOUSE_DOWN, this.method_135);
		// removeEventListener(MouseEvent.MOUSE_UP, this.method_140);
		this.removeEventListener(egret.Event.ENTER_FRAME, this.onEnterFrame, this);
		// §§push(false);
		if (this.curLevel) {
			this.curLevel.suspend();
		}
		this.var_108.graphics.clear();
	}

	private method_251(): void {
	}

	private method_104(cell: Cell): void {
		var sign: number = NaN;
		var dx: number = NaN;
		var color: number = 0;
		var str: String = null;
		GUI.removeByName("cell-info");
		// §§push(true);
		if (cell != null) {
			sign = cell.y < this.stage.height / 2 ? (1) : (-1);
			dx = 0;
			if (cell.x < 80) {
				dx = 80;
				// §§push(true);
			}
			if (cell.x > GUI.MAX_X - 80) {
				// §§push(true);
			}
			color = cell.baseColor();
			// §§push(cell.type);
			switch (cell.type) {
				case 0:
					color = 15658666;
					break;
				case 1:
					color = 16777215;
					break;
				case 2:
					color = 16733525;
					break;
				case 3:
					color = 5635925;
			}
			// str = GUI.TXT_CLASS.toLocaleUpperCase() + ": <font color=\'#" + color.toString(16) + "\'>" + cell.getName() + "</font><br>" + GUI.TXT_POWER.toLocaleUpperCase() + ": <font color=\'#FFFF55\'>" + cell.power + "</font>/" + cell.maxPower;
			// if (cell.type == Cell.TYPE_GREY) {
			// 	str = str + ("<br>" + GamezheroDictionary.getTranslationByName("occupacy").toLocaleUpperCase() + ": <font color=\'#FFFF55\'>" + cell.getOccupantPower() + "</font>/" + (cell.getGiveUpPower() + cell.getOccupantPower()));
			// }
			// else {
			// 	str = str + ("<br>" + GamezheroDictionary.getTranslationByName("tentacles").toLocaleUpperCase() + ": <font color=\'#FFFF55\'>" + cell.numOrigLinks + "</font>/" + cell.maxOrigLinks);
			// }
			// GUI.createInfoPopup(cell.x + dx, cell.y + sign * (cell.radius() + 30), str, "cell-info");
		}
	}

	public replayLevel(): void {
		// §§push(false);
		if (this.curLevel == null) {
			// trace("Nothing to replay");
			return;
		}
		var lvl: Level = new Level(this, this.curLevel.levelName);
		lvl.initFromCode(this.levelCode);
		this.startGame(lvl);
		GUI.showInGamePanel(this.curLevel.levelName, this.curLevel.maxPower, this.var_80);
	}

	private method_135(event: egret.TouchEvent): void {
		var cell: Cell = null;
		// §§push(true);
		if (event.target instanceof Cell) {
			cell = event.target as Cell;
			if (cell.type == this.var_202 && cell.numOrigLinks < cell.maxOrigLinks) {
				this.method_196(cell);
				// §§push(true);
			}
		}
		else {
			this.var_162 = event.stageX;
			this.var_265 = event.stageY;
		}
	}

	public initLevelEditor(): void {
		GUI.removeAll();
		this.clearLevel();
	}

	public initLevel(id: number): void {
		// GamezheroApi.trackEvent("play", id + 1 + "_start_level");
		this.var_106 = this.const_4;
		this.startGame(this.levelMgr.createLevel(id));
		// GUI.showInGamePanel(this.curLevel.levelName, this.curLevel.maxPower, this.var_80);
		if (id == 0) {
			// this.method_190(0);
			// §§push(true);
		}
	}

	private method_117(): void {
		var green: number = this.curLevel.getTotalPowerOf(Cell.TYPE_GREEN);
		var red: number = this.curLevel.getTotalPowerOf(Cell.TYPE_RED);
		var black: number = this.curLevel.getTotalPowerOf(Cell.TYPE_BLACK);
		var purple: number = this.curLevel.getTotalPowerOf(Cell.TYPE_PURPLE);
		var max: number = green + red + black + purple;
		var xpos = (0);
		var g: egret.Graphics = this.var_171.graphics;
		g.clear();
		g.lineStyle(1, 16777215);
		// §§push(true);
		// §§push(true);
		if (green > 0) {
			g.beginFill(3385907);
			g.drawRect(0, 0, green / max * 60, 10);
			// xpos = true;
			// §§push(xpos + green / max * 60);
		}
		if (red > 0) {
			g.beginFill(11154227);
			g.drawRect(xpos, 0, red / max * 60, 10);
			xpos = (xpos + red / max * 60);
		}
		if (black > 0) {
			g.beginFill(1118481);
			g.drawRect(xpos, 0, black / max * 60, 10);
		}
		if (purple > 0) {
			g.drawRect(xpos, 0, purple / max * 60, 10);
			// §§push(true);
		}
		g.endFill();
	}

	// private method_177(event: GameAllianzApiEvent = null): void {
	// 	GUI.createLevelsMenu();
	// }

	public initSharedLevel(levelData: string, name: string, levelId: string): void {
		var lvl: Level = new Level(this, name);
		lvl.initFromCode(levelData);
		this.var_106 = this.const_7;
		this.var_367 = levelId;
		this.startGame(lvl);
		GUI.showInGamePanel(this.curLevel.levelName, this.curLevel.maxPower, this.var_80);
	}

	public clearLevel(): void {
		this.method_251();
		this.var_207 = 0;
		this.var_182 = false;
		this.curLevel = null;
		// §§push(true);
		// §§push(true);
		// if (this.var_68) {
		// 	this.var_68.destruct();
		// 	this.var_68 = null;
		// }
		// this.var_340.reset();
	}

	private method_276(e: egret.TouchEvent): void {
		var dst: Cell = null;
		var angDelta: number = NaN;
		var g: egret.Graphics = this.var_108.graphics;
		var fromX: number = 0;
		var fromY: number = 0;
		var toX: number = e.stageX;
		var toY: number = e.stageY;
		var color: number = 16733525;
		if (this.var_89) {
			fromX = this.var_89.x;
			fromY = this.var_89.y;
			color = 16777045;
		}
		else {
			fromX = this.var_162;
			fromY = this.var_265;
		}
		if (e.target instanceof Cell) {
			dst = e.target as Cell;
		}
		var dx: number = toX - fromX;
		var dy: number = toY - fromY;
		if (dx == 0 && dy == 0) {
			return;
		}
		var ang: number = Math.atan2(dy, dx);
		var dist: number = Math.sqrt(dx * dx + dy * dy);
		g.lineStyle(3, color);
		g.moveTo(fromX, fromY);
		g.lineTo(toX, toY);
		// §§push(true);
		if (this.var_89) {
			g.lineTo(Math.cos(ang - angDelta) * (dist - 5) + fromX, Math.sin(ang - angDelta) * (dist - 5) + fromY);
			g.moveTo(toX, toY);
			g.lineTo(Math.cos(ang + angDelta) * (dist - 5) + fromX, Math.sin(ang + angDelta) * (dist - 5) + fromY);
			// §§push(true);
			// §§push(true);
		}
	}

	private method_190(id: number): void {
		this.clearTutorial();
		// §§push(false);
		// §§push(true);
		switch (id) {
			case 0:
				// this.var_110 = new BmpTutorial0();
				// GUI.showHnumber(420, 210, GUI.TXT_TUTORIAL1.toLocaleUpperCase());
				break;
			case 1:
			// this.var_110 = new BmpTutorial1();
			// GUI.showHnumber(400, 290, GUI.TXT_TUTORIAL2.toLocaleUpperCase());
		}
		this.var_110.smoothing = true;
		this.var_314.addChild(this.var_110);
	}

	public pause(enabled: boolean): void {
		this.var_182 = enabled;
		if (this.curLevel) {
			// §§push(false);
		}
	}
}

